package com.nike.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.ProxySelector;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.Builder;
import java.time.Duration;
import java.net.http.HttpResponse;
import java.net.http.HttpClient.Version;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;

import com.nike.bean.AppChromeInfo;
import com.nike.bean.NikeAppStaticInfo;
import com.nike.bean.NikeHttpBean;
import com.nike.ssl.SSLSocketClient;

public class NikeHttpUtilNew {
	public static HttpClient initHttpClient(Map<String,String> userMap) {
		Authenticator authenticator = new Authenticator() {
			  @Override
			  protected PasswordAuthentication getPasswordAuthentication() {
			    return new PasswordAuthentication(userMap.get("name"), userMap.get("pass").toCharArray());
			  }
			};
		String ipAndPort = userMap.get("ipAndPort");
		String ip = ipAndPort.split(":")[0];
		int port = Integer.parseInt(ipAndPort.split(":")[1]);
		// TODO
//		CookieManager cookieManager = new CookieManager();
//		cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_NONE);
		HttpClient httpClient = HttpClient.newBuilder()
      	.version(Version.HTTP_2)
				.proxy(ProxySelector.of(new InetSocketAddress(ip, port)))
				.sslContext(SSLSocketClient.getSLLContext())
				.connectTimeout(Duration.ofSeconds(30))
//				.cookieHandler(cookieManager)
//	        	.followRedirects(Redirect.NORMAL)
				.authenticator(authenticator)
				.build();
		return httpClient;
	}
	
	public static void sendMyHttpPost(String httpUrl, String data) throws Exception{
	      HttpClient httpClient = HttpClient.newBuilder()
					.connectTimeout(Duration.ofSeconds(30))
					.build();
			URI uri = new URI(httpUrl);
			Builder httpGetBuild = HttpRequest.newBuilder(uri);
			HttpRequest httpPostRequest = httpGetBuild
					.timeout(Duration.ofSeconds(30))
	              .POST(HttpRequest.BodyPublishers.ofString(data))
	              .build();
			httpClient.send(httpPostRequest, HttpResponse.BodyHandlers.ofString());
	}

	public static NikeHttpBean nikeHttpGetCheer(String url,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		URI uri = new URI(url);
		HttpRequest httpGetRequest = HttpRequest.newBuilder(uri)
			.header("Accept","application/vnd.nike.cheers-v3.4+json")
			.header("Accept","application/json")
			.header("Content-Type","application/json")
			.header("Authorization","Bearer "+userMap.get("access_token"))
			.header("X-NIKE-UX-ID", NikeAppStaticInfo.UX_ID)
			.header("Accept-Encoding", "gzip")
			.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
    		.GET()
            .build();
		httpClient.sendAsync(httpGetRequest, HttpResponse.BodyHandlers.ofInputStream())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
//                System.out.println(resp.version());
                nikeHttpBean.setStatusCode(resp.statusCode());
            	nikeHttpBean.setReturnContent(GzipUtils.zipToString(resp, userMap));          
            }
        }).join();
        
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpGetDemdex(String url,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		String version_name = userMap.get("version_name");
//		String mobile_name = userMap.get("mobile_name");
		String mobile_content = userMap.get("mobile_content");
//		String mobile_type = userMap.get("mobile_type");
		String build_id = userMap.get("build_id");
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		URI uri = new URI(url);
		HttpRequest httpGetRequest = HttpRequest.newBuilder(uri)
				.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
				.header("Accept-Language", "zh-CN")
				.header("User-Agent","Mozilla/5.0 (Linux; U; Android "+version_name+"; "+mobile_content+" Build/"+build_id+")")
    			.header("Accept-Encoding", "gzip")
    			.GET()
                .build();
		httpClient.sendAsync(httpGetRequest, HttpResponse.BodyHandlers.ofInputStream())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
//                System.out.println(resp.version());
                nikeHttpBean.setStatusCode(resp.statusCode());
            	nikeHttpBean.setReturnContent(GzipUtils.zipToString(resp, userMap));          
            }
        }).join();
        
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpPostSendorData(String url,String request_Body,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		URI uri = new URI(url);
		HttpRequest httpPostRequest = HttpRequest.newBuilder(uri)
		.header("Accept","application/json")
		.header("Authorization","Bearer "+userMap.get("access_token"))
		.header("X-NIKE-UX-ID", NikeAppStaticInfo.UX_ID)
		.header("X-acf-sensor-data", userMap.get("sensor_data"))
		.header("Content-Type","application/json; charset=UTF-8")
		
		.header("Accept-Encoding", "gzip")
		.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
		.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
		.POST((HttpRequest.BodyPublishers.ofString(request_Body)))
        .build();
		httpClient.sendAsync(httpPostRequest, HttpResponse.BodyHandlers.ofInputStream())
		.whenComplete((resp,t) -> {
		    if(t != null){
		        t.printStackTrace();
		    }else{
		//        System.out.println(resp.version());
		//        System.out.println(resp.statusCode());
		        nikeHttpBean.setStatusCode(resp.statusCode());
		        nikeHttpBean.setReturnContent(GzipUtils.zipToString(resp, userMap));
		    }
		}).join();
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpPutNotifications(String url,String request_Body,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		URI uri = new URI(url);
		HttpRequest httpPutRequest = HttpRequest.newBuilder(uri)
			.header("appid","com.nike.commerce.snkrs.droid")
			.header("upmId",userMap.get("upmId"))
			.header("deliveryId","com.nike.onenikecommerce")
			.header("Accept","application/json")
			.header("Authorization","Bearer "+userMap.get("access_token"))
			.header("Content-Type","application/json; charset=UTF-8")
			
			.header("Accept-Encoding", "gzip")
			.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
	    	.PUT(HttpRequest.BodyPublishers.ofString(request_Body))
	         .build();
		httpClient.sendAsync(httpPutRequest, HttpResponse.BodyHandlers.ofInputStream())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
//                System.out.println(resp.version());
//                System.out.println(resp.statusCode());
//                System.out.println(resp.body());
                nikeHttpBean.setReturnContent(GzipUtils.zipToString(resp, userMap));
            }
        }).join();
        
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpPostNoToken(String url,String request_Body,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		URI uri = new URI(url);
		HttpRequest httpPostRequest = HttpRequest.newBuilder(uri)
		.header("Accept","application/json")
		.header("Content-Type","application/json; charset=UTF-8")
		
		.header("Accept-Encoding", "gzip")
		.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
		.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
		.POST(HttpRequest.BodyPublishers.ofString(request_Body))
        .build();
		httpClient.send(httpPostRequest, HttpResponse.BodyHandlers.ofString());
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpPostNoSendorDataNoContent(String url,String request_Body,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		URI uri = new URI(url);
		HttpRequest httpPostRequest = HttpRequest.newBuilder(uri)
			.header("Accept","application/json")
			.header("Authorization","Bearer "+userMap.get("access_token"))
			.header("X-NIKE-UX-ID", NikeAppStaticInfo.UX_ID)
			.header("Content-Type","application/json; charset=UTF-8")
			
			.header("Accept-Encoding", "gzip")
			.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
	    	.POST(HttpRequest.BodyPublishers.ofString(request_Body))
	        .build();
		httpClient.send(httpPostRequest, HttpResponse.BodyHandlers.ofString());
//		System.out.println(response.version());
//		System.out.println(response.statusCode());
//      nikeHttpBean.setReturnContent(response.body());
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpPostDemdex(String url,String request_Body,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		URI uri = new URI(url);
		String version_name = userMap.get("version_name");
//		String mobile_name = userMap.get("mobile_name");
		String mobile_content = userMap.get("mobile_content");
//		String mobile_type = userMap.get("mobile_type");
		String build_id = userMap.get("build_id");
		HttpRequest httpPostRequest = HttpRequest.newBuilder(uri)
			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
			.header("Content-Type","application/x-www-form-urlencoded")
			.header("User-Agent","Mozilla/5.0 (Linux; U; Android "+version_name+"; "+mobile_content+" Build/"+build_id+")")
			.header("Accept-Language","zh-CN")
			
			.header("Accept-Encoding", "gzip")
    		.POST(HttpRequest.BodyPublishers.ofString(request_Body))
            .build();
		httpClient.send(httpPostRequest, HttpResponse.BodyHandlers.ofInputStream());
//		System.out.println(response.version());
//		System.out.println(response.statusCode());
//      nikeHttpBean.setReturnContent(response.body());
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpGet(String url,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		URI uri = new URI(url);
		HttpRequest httpGetRequest = HttpRequest.newBuilder(uri)
				.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
    			.header("Accept","application/json")
    			.header("Content-Type","application/json")
    			.header("Authorization","Bearer "+userMap.get("access_token"))
    			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
    			.header("Accept-Encoding", "gzip")
    			.header("X-NIKE-UX-ID", NikeAppStaticInfo.UX_ID)
    			.GET()
                .build();
		HttpResponse<InputStream> response =
				httpClient.send(httpGetRequest, HttpResponse.BodyHandlers.ofInputStream());
		nikeHttpBean.setReturnContent(GzipUtils.zipToString(response, userMap));    
		return nikeHttpBean;
	}
	
	public static NikeHttpBean nikeHttpGetNoToken(String url,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		
		String version_name = userMap.get("version_name");
//		String mobile_name = userMap.get("mobile_name");
		String mobile_content = userMap.get("mobile_content");
		String mobile_type = userMap.get("mobile_type");
		
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		URI uri = new URI(url);
		HttpRequest httpGetRequest = HttpRequest.newBuilder(uri)
    			.header("User-Agent","Dalvik/2.1.0 (Linux; U; Android "+version_name+"; "+mobile_content+" Build/"+mobile_type+")")
    			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
    			.header("Accept-Encoding", "gzip")
    			.GET()
                .build();
		httpClient.sendAsync(httpGetRequest, HttpResponse.BodyHandlers.ofInputStream())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
//                System.out.println(resp.version());
//                System.out.println(resp.statusCode());
//                System.out.println(resp.body());
                nikeHttpBean.setReturnContent(GzipUtils.zipToString(resp,userMap));
            }
        }).join();
        
		return nikeHttpBean;
	}
	
public static NikeHttpBean nikeHttpGetSF(String url,NikeHttpBean nikeHttpBean) throws Exception{
		
		URI uri = new URI(url);
		HttpRequest httpGetRequest = HttpRequest.newBuilder(uri)
				.header("User-Agent","Mozilla/5.0 (Linux; Android 5.1.1; DUK-AL20 Build/LMY48Z; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/52.0.2743.100 Safari/537.36")
    			.header("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
    			.header("Accept-Encoding", "gzip")
    			.header("Accept-Language", "zh-CN,en-US;q=0.8")
    			.GET()
                .build();
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		httpClient.sendAsync(httpGetRequest, HttpResponse.BodyHandlers.ofString())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
//                System.out.println(resp.version());
//                System.out.println(resp.statusCode());
                System.out.println(resp.body());
                nikeHttpBean.setStatusCode(resp.statusCode());
                nikeHttpBean.setReturnContent(resp.body());
            }
        }).join();
        
		return nikeHttpBean;
	}

public static NikeHttpBean nikeHttpGetOrderDetail(String url,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
	
	URI uri = new URI(url);
	HttpRequest httpGetRequest = HttpRequest.newBuilder(uri)
			.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
			.header("Authorization","Bearer "+userMap.get("access_token"))
			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
			.header("X-NIKE-UX-ID","com.nike.commerce.snkrs.droid")
			.header("Accept-Encoding", "gzip")
			.GET()
            .build();
	HttpClient httpClient = nikeHttpBean.getHttpClient();
	httpClient.sendAsync(httpGetRequest, HttpResponse.BodyHandlers.ofInputStream())
    .whenComplete((resp,t) -> {
        if(t != null){
            t.printStackTrace();
        }else{
//            System.out.println(resp.version());
//            System.out.println(resp.statusCode());
            nikeHttpBean.setStatusCode(resp.statusCode());
            nikeHttpBean.setReturnContent(GzipUtils.zipToString(resp, userMap));
        }
    }).join();
    
	return nikeHttpBean;
}
	public static NikeHttpBean nikeHttpGetOrder(String url,NikeHttpBean nikeHttpBean,Map<String,String> userMap,String refererUrl) throws Exception{
		
		URI uri = new URI(url);
		HttpRequest httpGetRequest = HttpRequest.newBuilder(uri)
				.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
    			.header("Accept","application/json")
    			.header("Content-Type","application/json")
    			.header("Authorization","Bearer "+userMap.get("access_token"))
    			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
    			.header("appid","orders")
    			.header("Accept-Encoding", "gzip")
    			.header("referer", refererUrl)
    			.GET()
                .build();
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		httpClient.sendAsync(httpGetRequest, HttpResponse.BodyHandlers.ofInputStream())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
//                System.out.println(resp.version());
//                System.out.println(resp.statusCode());
                nikeHttpBean.setStatusCode(resp.statusCode());
                nikeHttpBean.setReturnContent(GzipUtils.zipToString(resp, userMap));
            }
        }).join();
        
		return nikeHttpBean;
	}
public static NikeHttpBean nikeHttpPostOrder(String url,String postBody,NikeHttpBean nikeHttpBean,Map<String,String> userMap,String refererUrl) throws Exception{
		
		URI uri = new URI(url);
		HttpRequest httpPostRequest = HttpRequest.newBuilder(uri)
				.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
    			.header("Accept","application/json")
    			.header("Content-Type","application/json")
    			.header("Authorization","Bearer "+userMap.get("access_token"))
    			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
    			.header("appid","orders")
    			.header("Accept-Encoding", "gzip")
    			.header("referer", refererUrl)
    			.POST((HttpRequest.BodyPublishers.ofString(postBody)))
                .build();
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		httpClient.sendAsync(httpPostRequest, HttpResponse.BodyHandlers.ofInputStream())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
//                System.out.println(resp.version());
//                System.out.println(resp.statusCode());
                nikeHttpBean.setStatusCode(resp.statusCode());
                nikeHttpBean.setReturnContent(GzipUtils.zipToString(resp, userMap));
            }
        }).join();
        
		return nikeHttpBean;
	}

	public static NikeHttpBean nikeHttpGetLargeString(String url,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
	
		URI uri = new URI(url);
		HttpRequest httpGetRequest = HttpRequest.newBuilder(uri)
				.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
				.header("Accept","application/json")
				.header("Content-Type","application/json")
				.header("Authorization","Bearer "+userMap.get("access_token"))
				.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
				.header("Accept-Encoding", "gzip")
				.header("X-NIKE-UX-ID", NikeAppStaticInfo.UX_ID)
				.header("X-acf-sensor-data", userMap.get("sensor_data"))
				.GET()
	            .build();
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		httpClient.sendAsync(httpGetRequest, HttpResponse.BodyHandlers.ofInputStream())
	    .whenComplete((resp,t) -> {
	        if(t != null){
	            t.printStackTrace();
	        }else{
	//            System.out.println(resp.version());
	            nikeHttpBean.setStatusCode(resp.statusCode());
	            nikeHttpBean.setReturnContent(GzipUtils.zipToString(resp, userMap));
	        }
	    }).join();
	    
		return nikeHttpBean;
	}
//	public static NikeHttpBean nikeHttpGetSensorData(String url,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
//		
//		URI uri = new URI(url);
//		HttpRequest httpGetRequest = HttpRequest.newBuilder(uri)
//				.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
//    			.header("Accept","application/json")
//    			.header("Content-Type","application/json")
//    			.header("Authorization","Bearer "+userMap.get("access_token"))
//    			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
//    			.header("Accept-Encoding", "")
//    			.header("X-NIKE-UX-ID", NikeAppStaticInfo.UX_ID)
//    			.header("X-acf-sensor-data", userMap.get("sensor_data"))
//    			.GET()
//                .build();
//		HttpClient httpClient = nikeHttpBean.getHttpClient();
//		httpClient.sendAsync(httpGetRequest, HttpResponse.BodyHandlers.ofString())
//        .whenComplete((resp,t) -> {
//            if(t != null){
//                t.printStackTrace();
//            }else{
////                System.out.println(resp.version());
////                System.out.println(resp.statusCode());
////                System.out.println(resp.body());
//                nikeHttpBean.setStatusCode(resp.statusCode());
//                System.out.println(userMap.get("username")+"--"+resp.body());
//                nikeHttpBean.setReturnContent(resp.body());
//            }
//        }).join();
//        
//		return nikeHttpBean;
//	}
	public static NikeHttpBean nikeHttpGetSensorData(String url,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		return nikeHttpGetSensorDataGzip(url, nikeHttpBean, userMap);
	}
	public static NikeHttpBean nikeHttpGetSensorDataGzip(String url,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		
		URI uri = new URI(url);
		HttpRequest httpGetRequest = HttpRequest.newBuilder(uri)
				.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
    			.header("Accept","application/json")
    			.header("Content-Type","application/json")
    			.header("Authorization","Bearer "+userMap.get("access_token"))
    			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
    			.header("Accept-Encoding", "gzip")
    			.header("X-NIKE-UX-ID", NikeAppStaticInfo.UX_ID)
    			.header("X-acf-sensor-data", userMap.get("sensor_data"))
    			.GET()
                .build();
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		httpClient.sendAsync(httpGetRequest, HttpResponse.BodyHandlers.ofInputStream())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
//                System.out.println(resp.version());
                nikeHttpBean.setStatusCode(resp.statusCode());
                nikeHttpBean.setReturnContent(GzipUtils.zipToString(resp, userMap));
            }
        }).join();
        
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpGetSensorDataFile(String url,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		
		URI uri = new URI(url);
		HttpRequest httpGetRequest = HttpRequest.newBuilder(uri)
				.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
    			.header("Accept","application/json")
    			.header("Content-Type","application/json")
    			.header("Authorization","Bearer "+userMap.get("access_token"))
    			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
    			.header("Accept-Encoding", "gzip")
    			.header("X-NIKE-UX-ID", NikeAppStaticInfo.UX_ID)
    			.header("X-acf-sensor-data", userMap.get("sensor_data"))
    			.GET()
                .build();
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		httpClient.sendAsync(httpGetRequest, HttpResponse.BodyHandlers.ofString())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
//                System.out.println(resp.version());
//                System.out.println(resp.statusCode());
//                System.out.println(resp.body());
                nikeHttpBean.setStatusCode(resp.statusCode());
//                nikeHttpBean.setReturnContent(resp.body());
            }
        }).join();
        
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpGetSensorDataEtag(String url,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		
		URI uri = new URI(url);
		Builder builder = HttpRequest.newBuilder(uri);
		if(nikeHttpBean.getEtag()!=null && !"".equals(nikeHttpBean.getEtag())) {
			builder.header("If-None-Match",nikeHttpBean.getEtag());
		}
		HttpRequest httpGetRequest = builder
				.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
    			.header("Accept","application/json")
    			.header("Content-Type","application/json")
    			.header("Authorization","Bearer "+userMap.get("access_token"))
    			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
    			.header("Accept-Encoding", "gzip")
    			.header("X-NIKE-UX-ID", NikeAppStaticInfo.UX_ID)
    			.header("X-acf-sensor-data", userMap.get("sensor_data"))
    			.GET()
                .build();
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		httpClient.sendAsync(httpGetRequest, HttpResponse.BodyHandlers.ofInputStream())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
//                System.out.println(resp.version());
             
                List<String> ETaglist = resp.headers().allValues("ETag");
                for(String temp : ETaglist) {
                	System.out.println(temp);
                	nikeHttpBean.setEtag(temp);
                }
                nikeHttpBean.setStatusCode(resp.statusCode());
                nikeHttpBean.setReturnContent(GzipUtils.zipToString(resp, userMap));
            }
        }).join();
        
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpGet_2(String url,String referer,NikeHttpBean nikeHttpBean) throws Exception{
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		
		URI uri = new URI(url);
		HttpRequest httpGetRequest = HttpRequest.newBuilder(uri)
    			.header("User-Agent",nikeHttpBean.getPhone_type_name())
    			.header("Accept",AppChromeInfo.INDEX_ACCEPT)
    			.header("Referer",referer)
    			.GET()
                .build();
		List<String> cookies = httpGetRequest.headers().allValues("Cookie");
		for(String temp : cookies) {
			System.out.println("before request:"+temp);
		}
		httpClient.sendAsync(httpGetRequest, HttpResponse.BodyHandlers.ofString())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
//                System.out.println(resp.version());
//                System.out.println(resp.statusCode());
//                System.out.println(resp.body());
                List<String> list = resp.headers().allValues("set-cookie");
                for(String temp : list) {
                	System.out.println(temp);
                	if(temp.startsWith("bm_sz")) {
                		nikeHttpBean.setBm_sz(temp.split(";")[0]);
                	}else if(temp.startsWith("_abck")) {
                		nikeHttpBean.set_abck(temp.split(";")[0]);
                	}
                }
                nikeHttpBean.setReturnContent(resp.body());
            }
        }).join();
        
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpPostUnite(String url,String postBody,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		String version_name = userMap.get("version_name");
		String mobile_content = userMap.get("mobile_content");
//		String mobile_type = userMap.get("mobile_type");
		String build_id = userMap.get("build_id");
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		URI uri = new URI(url);
		HttpRequest httpPostRequest = HttpRequest.newBuilder(uri)
		.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
		.header("Accept-Language","UTF-8")
		.header("User-Agent","Dalvik/2.1.0 (Linux; U; Android "+version_name+"; "+mobile_content+" Build/"+build_id+")")
		.header("Accept-Encoding", "gzip")
		.header("Content-Type", "application/x-www-form-urlencoded")
		.POST(HttpRequest.BodyPublishers.ofString(postBody))
        .build();
		HttpResponse<InputStream> response =
				httpClient.send(httpPostRequest, HttpResponse.BodyHandlers.ofInputStream());
		nikeHttpBean.setReturnContent(GzipUtils.zipToString(response, userMap));
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpPostNoSendorData(String url,String request_Body,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		URI uri = new URI(url);
		HttpRequest httpPostRequest = HttpRequest.newBuilder(uri)
				.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
    			.header("Accept","application/json")
    			.header("Content-Type","application/json; charset=UTF-8")
    			.header("Authorization","Bearer "+userMap.get("access_token"))
    			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
    			.header("Accept-Encoding", "gzip")
    			.header("X-NIKE-UX-ID", NikeAppStaticInfo.UX_ID)
    			.POST(HttpRequest.BodyPublishers.ofString(request_Body))
                .build();
		HttpResponse<InputStream> response =
				httpClient.send(httpPostRequest, HttpResponse.BodyHandlers.ofInputStream());
//		System.out.println(response.version());
//		System.out.println(response.statusCode());
		System.out.println(response.body());
		nikeHttpBean.setReturnContent(GzipUtils.zipToString(response, userMap));
		return nikeHttpBean;
	}
	public static String uncompress(String str,String outEncoding) throws IOException {
        if (str == null || str.length() == 0) {
          return str;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
        GZIPInputStream gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n;
        while ((n = gunzip.read(buffer)) >= 0) {
          out.write(buffer, 0, n);
        }
        return out.toString(outEncoding);
    }
	public static NikeHttpBean nikeHttpPostGzip(String url,String request_Body,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		
		URI uri = new URI(url);
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		Builder builder = HttpRequest.newBuilder(uri);
		if(nikeHttpBean.getEtag()!=null && !"".equals(nikeHttpBean.getEtag())) {
			builder.header("If-None-Match",nikeHttpBean.getEtag());
		}
		builder.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
		.header("Accept","application/json")
		.header("Content-Type","application/json; charset=UTF-8")
		.header("Authorization","Bearer "+userMap.get("access_token"))
		.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
		.header("Accept-Encoding", "gzip")
		.header("X-NIKE-UX-ID", NikeAppStaticInfo.UX_ID)
		.header("X-acf-sensor-data", userMap.get("sensor_data"))
		.POST(HttpRequest.BodyPublishers.ofString(request_Body));
		HttpRequest httpPostRequest =  builder.build();
		HttpResponse<InputStream> response =
				httpClient.send(httpPostRequest, HttpResponse.BodyHandlers.ofInputStream());
//		System.out.println(response.version());
		System.out.println(response.statusCode());
//		CompressUtils.uncompressToString(Base64.getDecoder().decode(response.body()));
		System.out.println(response.body());
//		System.out.println(uncompress(response.body(),"utf-8"));
//		List<String> list = response.headers().allValues("Location");
//	      for(String temp : list) {
//	      	System.out.println(temp);
//	      	if(temp.startsWith("bm_sz")) {
//	      		nikeHttpBean.setBm_sz(temp.split(";")[0]);
//	      	}else if(temp.startsWith("_abck")) {
//	      		nikeHttpBean.set_abck(temp.split(";")[0]);
//	      	}
//	      }
	  nikeHttpBean.setStatusCode(response.statusCode());
      nikeHttpBean.setReturnContent(GzipUtils.zipToString(response, userMap));
		return nikeHttpBean;
	}
	
	public static NikeHttpBean nikeHttpPostContentType(String url,String request_Body,NikeHttpBean nikeHttpBean,Map<String,String> userMap,String contentType) throws Exception{
		
		URI uri = new URI(url);
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		HttpRequest httpPostRequest = HttpRequest.newBuilder(uri)
				.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
    			.header("Accept","application/json")
    			.header("Content-Type",contentType)
    			.header("Authorization","Bearer "+userMap.get("access_token"))
    			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
    			.header("Accept-Encoding", "gzip")
    			.header("X-NIKE-UX-ID", NikeAppStaticInfo.UX_ID)
//    			.header("X-acf-sensor-data", userMap.get("sensor_data"))
    			.POST(HttpRequest.BodyPublishers.ofString(request_Body))
                .build();
		HttpResponse<InputStream> response =
				httpClient.send(httpPostRequest, HttpResponse.BodyHandlers.ofInputStream());
		System.out.println(response.body());
		nikeHttpBean.setReturnContent(GzipUtils.zipToString(response, userMap));
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpPost(String url,String request_Body,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		
		URI uri = new URI(url);
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		HttpRequest httpPostRequest = HttpRequest.newBuilder(uri)
				.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
    			.header("Accept","application/json")
    			.header("Content-Type","application/json; charset=UTF-8")
    			.header("Authorization","Bearer "+userMap.get("access_token"))
    			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
    			.header("Accept-Encoding", "gzip")
    			.header("X-NIKE-UX-ID", NikeAppStaticInfo.UX_ID)
    			.header("X-acf-sensor-data", userMap.get("sensor_data"))
    			.POST(HttpRequest.BodyPublishers.ofString(request_Body))
                .build();
		httpClient.sendAsync(httpPostRequest, HttpResponse.BodyHandlers.ofInputStream())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
//                System.out.println(resp.version());
//                System.out.println(resp.statusCode());
//                System.out.println(resp.body());
                nikeHttpBean.setReturnContent(GzipUtils.zipToString(resp, userMap));
            }
        }).join();
//		HttpResponse<String> response =
//				httpClient.send(httpPostRequest, HttpResponse.BodyHandlers.ofString());
////		System.out.println(response.version());
////		System.out.println(response.statusCode());
////		CompressUtils.uncompressToString(Base64.getDecoder().decode(response.body()));
//		System.out.println(response.body());
////		List<String> list = response.headers().allValues("Location");
////	      for(String temp : list) {
////	      	System.out.println(temp);
////	      	if(temp.startsWith("bm_sz")) {
////	      		nikeHttpBean.setBm_sz(temp.split(";")[0]);
////	      	}else if(temp.startsWith("_abck")) {
////	      		nikeHttpBean.set_abck(temp.split(";")[0]);
////	      	}
////	      }
//      nikeHttpBean.setReturnContent(response.body());
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpPut(String url,String request_Body,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		URI uri = new URI(url);
		HttpRequest httpPutRequest = HttpRequest.newBuilder(uri)
				.header("User-Agent",AndroidUtil.initUserAngentFromUser(userMap))
    			.header("Accept","application/json")
//    			.header("upmId",userMap.get("user_id"))
    			.header("Content-Type","application/json; charset=UTF-8")
    			.header("Authorization","Bearer "+userMap.get("access_token"))
    			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
    			.header("Accept-Encoding", "gzip")
    			.header("X-NIKE-UX-ID", NikeAppStaticInfo.UX_ID)
    			.header("X-acf-sensor-data", userMap.get("sensor_data"))
    			.PUT(HttpRequest.BodyPublishers.ofString(request_Body))
                .build();
		httpClient.sendAsync(httpPutRequest, HttpResponse.BodyHandlers.ofInputStream())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
//                System.out.println(resp.version());
//                System.out.println(resp.statusCode());
//                System.out.println(resp.body());
                nikeHttpBean.setReturnContent(GzipUtils.zipToString(resp, userMap));
            }
        }).join();
        
		return nikeHttpBean;
	}
	public static NikeHttpBean nikeHttpDelete(String url,NikeHttpBean nikeHttpBean,Map<String,String> userMap) throws Exception{
		HttpClient httpClient = nikeHttpBean.getHttpClient();
		URI uri = new URI(url);
		HttpRequest httpPutRequest = HttpRequest.newBuilder(uri)
				.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
    			.header("Accept","*/*")
    			.header("Referer","https://www.nike.com/member/settings/addresses")
    			.header("Authorization","Bearer "+userMap.get("access_token"))
    			.header("X-NewRelic-ID",NikeAppStaticInfo.NewRelic_ID)
    			.header("Accept-Encoding", "gzip")
    			.header("X-NIKE-UX-ID", NikeAppStaticInfo.UX_ID)
    			.DELETE()
                .build();
		httpClient.sendAsync(httpPutRequest, HttpResponse.BodyHandlers.ofString())
        .whenComplete((resp,t) -> {
            if(t != null){
                t.printStackTrace();
            }else{
                System.out.println(resp.body());
                nikeHttpBean.setReturnContent(resp.body());
            }
        }).join();
        
		return nikeHttpBean;
	}
	
}
